<?php

namespace App\Service\Cruise\Mission;

use App\Models\Cruise\CruiseCheckRectifyModel;
use App\Models\Cruise\CruiseCheckRiskProblemModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Support\CustomForm\AttributeCruiseSupport;

class CommonInputService extends CruiseOrderInputBaseService
{
    public function __construct(string $orderId, int $templateType, int $sysTemplate, ?CruiseOrderModel $cruiseOrderModel = null)
    {
        parent::__construct($orderId, $templateType, $sysTemplate, $cruiseOrderModel);
    }

    protected function initOrderInput(): void
    {
        // TODO: Implement initOrderInput() method.
    }

    protected function saveUserContentJson(string $field, $value): void
    {
        // TODO: Implement saveUserContentJson() method.
    }

    public function getInputObjectFill(): array
    {
        return parent::baseGetInputObjectFill(false);
    }

    protected function submitOrder(): void
    {
        // TODO: Implement submitOrder() method.
    }

    protected function audit(array $data): void
    {
        // TODO: Implement audit() method.
    }

    private array $project = [];
    public function setProject(array $project)
    {
        $this->project = $project;

        return $this;
    }

    /**
     * 项目的检查结果
     * @param array $project
     * @return string
     */
    public function checkResult(): string
    {
        $value = $this->project['value'] ?? null;
        if ($value === null) {
            return '未填写';
        }

        return collect($this->project['customAttributes']['options'])->where('value', $value)->first()['text'] ?? '';
    }

    /**
     * 稽核图片
     * @param array $project
     * @return string
     */
    public function checkImages(): string
    {
        $value = $this->project['value'] ?? null;
        if ($value === null) {
            return '未填写';
        }

        if ($value === AttributeCruiseSupport::CRUISE_CHECK_SKIP) {
            return '-';
        }

        return collect($this->project['customAttributes']['auditValue']['files'] ?? [])->pluck('url')->implode('、');
    }

    public function desc(): string
    {
        $value = $this->project['value'] ?? null;
        if ($value === null) {
            return '未填写';
        }

        // 合格 - 备注
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_OK) {
            return $this->project['customAttributes']['auditValue']['remark'] ?? '';

        }

        // 不合格 - 问题描述
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_FAIL) {
            return $this->project['customAttributes']['auditValue']['desc'] ?? '';
        }

        // 不适用 - 不适用原因
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_SKIP) {
            return $this->project['customAttributes']['auditValue']['skipDesc'] ?? '';
        }

        return '';
    }

    public function problemDesc(int $riskType2): array
    {
        $value = $this->project['value'] ?? null;
        if ($value === null) {
            return ['未填写'];
        }

        // 合格 - 备注
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_OK) {
            return [$this->project['customAttributes']['auditValue']['remark'] ?? ''];

        }

        // 不合格 - 问题描述
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_FAIL) {

            $problems = collect($this->project['customAttributes']['auditValue']['problems'] ?? [])->keyBy('riskType2');
            $problemsIds = $problems[$riskType2]['problemIds'] ?? [];

            $result = [];

            foreach ($problemsIds as $problemsId) {
                $problemName = CruiseCheckRiskProblemModel::getName('problemName', $problemsId);

                $str = $problemName;

                // problemName前两个字符串
                $problemNamePrefix = mb_substr($problemName, 0, 2);
                if ($problemNamePrefix == "其他") {
                    $str .= "：" . ($problems[$riskType2]['elseProblem'] ?? '');
                }

                $result[] = $str;
            }

            return $result;
        }

        // 不适用 - 不适用原因
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_SKIP) {
            return [$this->project['customAttributes']['auditValue']['skipDesc'] ?? ''];
        }

        return [''];
    }

    /**
     * 整改类型
     * @return string
     */
    public function rectifyStatusText(): string
    {
        $value = $this->project['value'] ?? null;
        if ($value === null) {
            return '未填写';
        }

        // 合格
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_OK) {
            return '-';
        }

        // 不合格
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_FAIL) {
            $rectifyStatus = $this->project['customAttributes']['auditValue']['rectifyStatus'] ?? AttributeCruiseSupport::RECTIFY_STATUS_UNDONE;

            return AttributeCruiseSupport::RECTIFY_STATUS_TEXT[$rectifyStatus] ?? '';
        }

        // 不适用
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_SKIP) {
            return '-';
        }

        return '';
    }

    /**
     * 整改建议
     * @return string
     */
    public function rectifyDesc(): string
    {
        $value = $this->project['value'] ?? null;
        if ($value === null) {
            return '未填写';
        }

        // 合格
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_OK) {
            return '-';
        }

        // 不合格 - 整改建议
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_FAIL) {
            return $this->project['customAttributes']['auditValue']['reason'] ?? '';
        }

        // 不适用
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_SKIP) {
            return '-';
        }

        return '';
    }

    /**
     * 现场整改图片
     * @return string
     */
    public function rectifyImages(): string
    {
        $value = $this->project['value'] ?? null;
        if ($value === null) {
            return '未填写';
        }

        // 合格
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_OK) {
            return '-';
        }

        // 不合格 - 现场整改图片
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_FAIL) {
            return collect($this->project['customAttributes']['auditValue']['rectifyFiles'] ?? [])->pluck('url')->implode('、');
        }

        // 不适用
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_SKIP) {
            return '-';
        }

        return '';
    }

    /**
     * 最新整改时间
     * @param $cruiseCheckRectify
     * @return string
     */
    public function lastSubmitTime($cruiseCheckRectify): string
    {
        $value = $this->project['value'] ?? null;
        if ($value === null) {
            return '未填写';
        }

        // 合格
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_OK) {
            return '-';
        }

        // 不合格 - 整改状态
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_FAIL) {

//            $status = $cruiseCheckRectify['status'] ?? 0;
//            if ($status == CruiseCheckRectifyModel::STATUS_WAIT_AUDIT) {

                if ($cruiseCheckRectify && $cruiseCheckRectify->replyRel->count()) {
                    return $cruiseCheckRectify->replyRel->last()->createdAt->toDateTimeString();
                }
//            }

            return '-';
        }

        // 不适用
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_SKIP) {
            return '-';
        }

        return '';
    }

    /**
     * 整改状态
     * @return string
     */
    public function rectifyOrderStatus($cruiseCheckRectify): string
    {
        $value = $this->project['value'] ?? null;
        if ($value === null) {
            return '未填写';
        }

        // 合格
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_OK) {
            return '-';
        }

        // 不合格 - 整改状态
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_FAIL) {

            $auditValueRectifyStatus = $this->project['customAttributes']['auditValue']['rectifyStatus'] ?? AttributeCruiseSupport::RECTIFY_STATUS_UNDONE;
            if ($auditValueRectifyStatus == AttributeCruiseSupport::RECTIFY_STATUS_DONE) {
                return '-';
            }

            return CruiseCheckRectifyModel::STATUS_TEXT[$cruiseCheckRectify['status'] ?? 0] ?? '';
        }

        // 不适用
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_SKIP) {
            return '-';
        }

        return '';
    }

    /**
     * 整改审核结果
     * @return string
     */
    public function rectifyAuditResult($cruiseCheckRectify): string
    {
        $value = $this->project['value'] ?? null;
        if ($value === null) {
            return '未填写';
        }

        // 合格
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_OK) {
            return '-';
        }

        // 不合格 - 整改状态
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_FAIL) {

            $status = $cruiseCheckRectify['status'] ?? 0;
            if ($status == CruiseCheckRectifyModel::STATUS_FINISH) {
                return '已通过';
            }

            // 有过提交记录 && 待整改
            if ($cruiseCheckRectify && $cruiseCheckRectify->replyRel->count() && $status == CruiseCheckRectifyModel::STATUS_WAIT_SUBMIT) {
                return '已驳回';
            }

            return '-';
        }

        // 不适用
        if ($value == AttributeCruiseSupport::CRUISE_CHECK_SKIP) {
            return '-';
        }

        return '-';
    }
}
